package ru.testtask.util;

import java.util.HashSet;
import java.util.Set;

public class Config {

    private static Config instance;

    private boolean anyElemName;
    private Set<String> elemNames;
    private boolean anyAttrName;
    private Set<String> attrNames;

    private Config(String elemNamesStr, String attrNamesStr) {
        if ("*".equals(elemNamesStr)) {
            anyElemName = true;
        } else {
            String[] elemNames = elemNamesStr.split(",");
            this.elemNames = new HashSet<String>(elemNames.length);
            for (String elemName : elemNames) {
                this.elemNames.add(elemName.trim());
            }
        }

        if ("*".equals(attrNamesStr)) {
            anyAttrName = true;
        } else {
            String[] attrNames = attrNamesStr.split(",");
            this.attrNames = new HashSet<String>(attrNames.length);
            for (String attrName : attrNames) {
                this.attrNames.add(attrName.trim());
            }
        }
    }

    public static Config getInstance() {
        if (instance == null) {
            throw new RuntimeException("intance is not created");
        }
        return instance;
    }

    public static Config createInstance(String elemNames, String attrNames) {
        if (instance == null) {
            instance = new Config(elemNames, attrNames);
        }
        return instance;
    }

    public boolean isElementNameAllowed(String name) {
        return anyElemName || elemNames.contains(name);
    }

    public boolean isAttributeNameAllowed(String name) {
        return anyAttrName || attrNames.contains(name);
    }
}
